package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME)
        //resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端请求的字符编码集为utf-8
        //req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //获取表单数据封装到对象中
        Guestbook gb = new Guestbook();
        gb.setName(req.getParameter("name"));
        gb.setPhone(req.getParameter("phone"));
        gb.setEmail(req.getParameter("email"));
        gb.setTitle(req.getParameter("title"));
        gb.setContent(req.getParameter("content"));
        //创建留言的时间（系统时间）
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        gb.setCreatedtime(simpleDateFormat.format(new Date()));
        //调用Service
        GuestbookService service = new GuestbookServiceImpl();
        boolean isRight = service.addInfo(gb);
        if(isRight){
            //System.out.println("<a href='/JavaWebPro/user/manager?q=list'>查看所有的留言信息</a>");
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
//      resp.sendRedirect("http://www.163.com");
//      resp.sendRedirect(req.getContextPath()+"/user/manager?q=list");
        }else {
            out.println("error......");
        }
    }
}